package com.igoal.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.igoal.entity.Category;
import com.igoal.entity.DataStatus;
import com.igoal.entity.Goal;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class GoalDTO extends BaseDTO<Goal> implements Serializable {
	private static final long serialVersionUID = 5545079420775429109L;

	private Long id;
	private String title;
	private int userCount;
	private int doneCount;
	private int cloudIndex;
	private String category;
	private Long categoryId;
	private List<CategoryDTO> categories;
	private DataStatus status;

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getUserCount() {
		return userCount;
	}

	public void setUserCount(int userCount) {
		this.userCount = userCount;
	}
	
	public int getDoneCount() {
		return doneCount;
	}

	public void setDoneCount(int doneCount) {
		this.doneCount = doneCount;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getCloudIndex() {
		return cloudIndex;
	}

	public void setCloudIndex(int cloudIndex) {
		this.cloudIndex = cloudIndex;
	}
	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public List<CategoryDTO> getCategories() {
		return categories;
	}

	public void setCategories(List<CategoryDTO> categories) {
		this.categories = categories;
	}

	public DataStatus getStatus() {
		return status;
	}

	public void setStatus(DataStatus status) {
		this.status = status;
	}

	@SuppressWarnings("unchecked")
	@SynchronizeAttributeWithOther(direction=SynchronizeDirection.FROM, value="categories")
	public void initGoalCategory(Object entityValue){
		if(entityValue != null && entityValue instanceof Set<?>){
			Set<Category> categories = (Set<Category>) entityValue;
			List<Category> listCategory = new ArrayList<Category>(categories);
			if(!listCategory.isEmpty()){
				this.category = listCategory.get(0).getCategory();
				this.categoryId = listCategory.get(0).getId();
				this.categories = new ArrayList<CategoryDTO>();				
				for(Category category : categories){
					CategoryDTO dto = new CategoryDTO();
					dto.initFrom(category);
					this.categories.add(dto);
				}
			}
		}
	}
}
